"""
Case Type   : gs_checkse工具功能
Case Name   : 验证gs_checkse的A9检测项,确保文件权限掩码配置检测成功
Create At   : 2024/11/1
Owner       : lihongji
Description :
    1.修改$HOME/.bashrc文件中的umask的值为0075后检测A9项
    2.对不符合安全规范的A9修复后检测,并查看umask的值
Expect      :
    1.设置成功，检测打印告警信息
    2.修复成功，不打印告警信息，umask的值为0077
History     :
"""
import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class GS_CHECKSE(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primaryNode = Node('PrimaryDbUser')
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.env_path = macro.DB_ENV_PATH

    def test_gscheckse(self):
        step = ('----step1:修改$HOME/.bashrc文件中的umask的值为0075后检测A9项'
                'expect:设置成功，检测打印告警信息----')
        self.log.info(step)
        gs_checkse_cmd = (f'cd ~;'
                          f'sed -i "\$a\\umask 0075" .bashrc;'
                          f'source {self.env_path};'
                          f'source .bashrc;'
                          f'gs_checkse -i A9 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn("Ensure correct file permission mask configuration.",
                      check_res, '执行失败' + step)

        step = ('----step2:修复A9不符合安全项再次检测,查看umask值'
                'expect:修复成功,不打印告警信息,umask值为0077----')
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'cd ~;'
                          f'source .bashrc;'
                          f'gs_checkse -i B9 --detail;' 
                          f'source .bashrc;'
                          f'gs_checkse -i A9 --detail;'
                          f'umask;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertIn('0077', check_res, '执行失败' + step)
        self.assertIn("[ Set Runtime environment configuration ]       "
                      "        : Normal", check_res, '执行失败' + step)
        self.assertNotIn("Ensure correct file permission mask configuration.",
                      check_res, '执行失败' + step)

    def tearDown(self):
        step = '----step3:清理环境 expect:成功----'
        self.log.info(step)
        revoke = (f'cd ~;'
                  f'sed -i "/umask/d" .bashrc;')
        self.log.info(revoke)
        self.common.get_sh_result(self.primaryNode, revoke)
        cat_mask = ('cd ~;'
                    'cat .bashrc | grep umask;')
        self.log.info(cat_mask)
        res_mask = self.common.get_sh_result(self.primaryNode, cat_mask)
        self.assertEqual('', res_mask, '执行失败' + step)
        self.log.info(f'----{os.path.basename(__file__)} end----')